package activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.searchparking.R;
import com.searchparking.BUS.GMapV2Direction;
import com.searchparking.BUS.GetDirectionsAsyncTask;
import com.searchparking.DTO.InfoObject;

import android.graphics.Color;

import android.os.Bundle;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MapViewDirection extends FragmentActivity {
	private GoogleMap map;
	private SupportMapFragment fragment;
	private LatLngBounds latlngBounds;
	private boolean check = false;
	private Button bNavigation;
	private Polyline newPolyline;

	private int width, height;
	private Double current_latitude, current_longitude, disten_latitude,
			disten_longitude, disten_latitude_map, disten_longitude_map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_view_direction);
		Intent i = new Intent();
		Bundle bundle = getIntent().getExtras();
		current_latitude = MainActivity.latitude;
		current_longitude = MainActivity.longitude;
		disten_latitude = bundle.getDouble("DeLatitude", 1);
		disten_longitude = bundle.getDouble("DeLongtati", 1);
		//
		disten_latitude_map = bundle.getDouble("DesLatitude_map");
		disten_longitude_map = bundle.getDouble("DesLongitude_map");
		if (disten_latitude_map != 0)
			check = true;
		getSreenDimanstions();
		fragment = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map_direction));
		map = fragment.getMap();

		bNavigation = (Button) findViewById(R.id.bNavigation);
		bNavigation.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (check) {
					addMarkertoMap_curent(new LatLng(current_latitude,
							current_longitude), map, "A");
					addMarkertoMap_destination(
							new LatLng(disten_latitude_map, disten_longitude_map), map,
							"B");
					findDirections(current_latitude, current_longitude,
							disten_latitude_map, disten_longitude_map,
							GMapV2Direction.MODE_DRIVING);
				} else {
					addMarkertoMap_curent(new LatLng(current_latitude,
							current_longitude), map, "A");
					addMarkertoMap_destination(
							new LatLng(disten_latitude, disten_longitude), map,
							"B");

					findDirections(current_latitude, current_longitude,
							disten_latitude, disten_longitude,
							GMapV2Direction.MODE_DRIVING);
				}

				Log.d("Vi tri",
						String.valueOf(disten_latitude) + " "
								+ String.valueOf(disten_longitude));
			}
		});
	}

	@Override
	protected void onResume() {

		super.onResume();
		latlngBounds = createLatLngBoundsObject(new LatLng(current_latitude,
				current_longitude), new LatLng(disten_latitude,
				disten_longitude));
		map.moveCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width,
				height, 150));

	}

	public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
		PolylineOptions rectLine = new PolylineOptions().width(5).color(
				Color.BLUE);

		for (int i = 0; i < directionPoints.size(); i++) {
			rectLine.add(directionPoints.get(i));
		}
		if (newPolyline != null) {
			newPolyline.remove();
		}
		newPolyline = map.addPolyline(rectLine);

		map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds,
				width, height, 150));

	}

	private void getSreenDimanstions() {
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
	}

	private LatLngBounds createLatLngBoundsObject(LatLng firstLocation,
			LatLng secondLocation) {
		if (firstLocation != null && secondLocation != null) {
			LatLngBounds.Builder builder = new LatLngBounds.Builder();
			builder.include(firstLocation).include(secondLocation);

			return builder.build();
		}
		return null;
	}

	public void findDirections(double fromPositionDoubleLat,
			double fromPositionDoubleLong, double toPositionDoubleLat,
			double toPositionDoubleLong, String mode) {
		Map<String, String> map = new HashMap<String, String>();
		map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT,
				String.valueOf(fromPositionDoubleLat));
		map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG,
				String.valueOf(fromPositionDoubleLong));
		map.put(GetDirectionsAsyncTask.DESTINATION_LAT,
				String.valueOf(toPositionDoubleLat));
		map.put(GetDirectionsAsyncTask.DESTINATION_LONG,
				String.valueOf(toPositionDoubleLong));
		map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);

		GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this);
		asyncTask.execute(map);
	}

	private void addMarkertoMap_curent(LatLng point, GoogleMap mapGoogle, String title) {

		Marker markerCustom = mapGoogle.addMarker(new MarkerOptions().position(
				point).
				icon(BitmapDescriptorFactory.fromResource(R.drawable.icmarker_current))
				.title(title));

	}
	private void addMarkertoMap_destination(LatLng point, GoogleMap mapGoogle, String title) {

		Marker markerCustom = mapGoogle.addMarker(new MarkerOptions().position(
				point).
				icon(BitmapDescriptorFactory.fromResource(R.drawable.icmarker_destination))
				.title(title));

	}
}
